package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmmaterialaidline extends X_hrm_materialaid_line {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2663704857037021L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmmaterialaidline.class);
	
	
	public Mhrmmaterialaidline(Properties ctx, int hrm_materialaid_line_ID,
			String trxName) {
		super(ctx, hrm_materialaid_line_ID, trxName);
		// 
	}

	public Mhrmmaterialaidline(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// 
	}

	public static Mhrmmaterialaidline getAidRec(int C_BPartner_ID, Timestamp dt) {
		
		if (C_BPartner_ID == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmmaterialaidline hrmmaidline = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_MATERIALAID_LINE \n " +
						"WHERE ISACTIVE='Y' \n " +
							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							"AND DATEFROM BETWEEN DATE_TRUNC('MONTH', " + DB.TO_DATE(dt, true) + ") " +
							"AND DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") + '1 MON'::INTERVAL - '1 DAY'::INTERVAL";							
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				if(rs.next())
					hrmmaidline = new Mhrmmaterialaidline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}	
		return hrmmaidline;
		
	}
	
	public static Mhrmmaterialaidline[] getAids(int C_BPartner_ID, Timestamp dt) {
		
		if (C_BPartner_ID == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmmaterialaidline result = null;
		
		ArrayList<Mhrmmaterialaidline> list = new ArrayList<Mhrmmaterialaidline>();
			
		// 
		String sql_ = "SELECT * FROM HRM_MATERIALAID_LINE \n " +
						"WHERE ISACTIVE='Y' \n " + 
							" AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							" AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							" AND DATEFROM BETWEEN DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") \n " +
							" AND DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") + '1 MON'::INTERVAL - '1 DAY'::INTERVAL";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					result = new Mhrmmaterialaidline(Env.getCtx(), rs, null);
					list.add(result);
				}				
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return list.toArray(new Mhrmmaterialaidline[list.size()]);
	
	}
	
	/**	Materialaid Lines	*/
	private Mhrmmaterialaidline[] m_lines;
	
	/**
	 * 	Get HR Materialaid Lines
	 * 	@param whereClause starting with AND
	 * 	@return lines
	 */
	private Mhrmmaterialaidline[] getLines (String whereClause)
	{
		String whereClauseFinal = "hrm_materialaid_ID=? ";
		if (whereClause != null)
			whereClauseFinal += whereClause;
		List<Mhrmmaterialaidline> list = new Query(getCtx(), I_hrm_materialaid_line.Table_Name, whereClauseFinal, get_TrxName())
										.setParameters(gethrm_materialaid_ID())
										.list();
		return list.toArray(new Mhrmmaterialaidline[list.size()]);
	}	//	getLines

	/**
	 * 	Get Materialaid Lines
	 * 	@param requery
	 * 	@return lines
	 */
	public Mhrmmaterialaidline[] getLines (boolean requery)
	{
		if (m_lines == null || m_lines.length == 0 || requery)
			m_lines = getLines(null);
		set_TrxName(m_lines, get_TrxName());
		return m_lines;
	}	//	getLines

	/**
	 * 	Get Materialaid line
	 * 	@return lines
	 */
	public Mhrmmaterialaidline[] getLines()
	{
		return getLines(false);
	}	//	getLines
	
	public static X_hrm_materialaidline getline(int hrm_materialaid_id, int C_BPartner_ID, int C_Charge_ID){
		
		if (hrm_materialaid_id==0)
			return null;

		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		X_hrm_materialaidline result = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_MATERIALAIDLINE \n " +
						"WHERE ISACTIVE='Y' \n " +
//							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND C_CHARGE_ID=" + C_Charge_ID + " \n " +
							"AND hrm_materialaid_id=" + hrm_materialaid_id + " \n " +
							"AND C_BPartner_ID =" + C_BPartner_ID ;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new X_hrm_materialaidline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
}
